package com.menu.controller;

import com.menu.model.User;
import com.menu.result.Result;
import com.menu.result.ResultFactory;
import com.menu.service.UserService;
import com.menu.redis.StringRedisServiceImpl;
import com.menu.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * author: MaHX
 * date: 2019/8/9/1:37
 * description:
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    StringRedisServiceImpl stringRedisService;

    @PostMapping("loginByUaP")
    public Result loginByUaP(@RequestBody User user, HttpSession session) {
        if (userService.findUserByUsername(user.getUsername()) == null) {
            return ResultFactory.buildFailResult("用户为注册");
        } else if (userService.find(user) != null) {
            session.setAttribute("user", userService.findUserByUsername(user.getUsername()));
            return ResultFactory.buildSuccessResult(null);
        } else {
            return ResultFactory.buildFailResult("账号或密码错误");
        }
    }

    @PostMapping("loginByPaC")
    public Result loginByPaC(@RequestBody User user, HttpSession session) {
        String trueCode;
        if (stringRedisService.hasKey(user.getPhone())) {
            trueCode = (String) stringRedisService.get(user.getPhone());
        } else {
            return ResultFactory.buildFailResult("验证码失效");
        }
        if (userService.findUserByPhone(user.getPhone()) == null) {
            return ResultFactory.buildFailResult("手机号码未绑定");
        }else if (!trueCode.equals(user.getCode())) {
            return ResultFactory.buildFailResult("验证码错误");
        }else {
            session.setAttribute("user",userService.findUserByPhone(user.getPhone()));
            return ResultFactory.buildSuccessResult(null);
        }
    }

    @PostMapping("register")
    public Result register(@RequestBody User user,HttpSession session) {
        String trueCode;
        if (stringRedisService.hasKey(user.getPhone())) {
            trueCode = (String) stringRedisService.get(user.getPhone());
        } else {
            return ResultFactory.buildFailResult("验证码失效");
        }
        if (userService.findUserByPhone(user.getPhone()) != null) {
            return ResultFactory.buildFailResult("该手机已绑定");
        } else if (!trueCode.equals(user.getCode())) {
            return ResultFactory.buildFailResult("验证码错误");
        } else {
            user.setUsername("");
            user.setPassword("");
            user.setGender("男");
            session.setAttribute("user", userService.findUserByPhone(user.getPhone()));
            userService.save(user);
            return ResultFactory.buildSuccessResult(null);
        }
    }

    @PostMapping("save")
    public Result save(@RequestBody User user) {
        userService.save(user);
        return ResultFactory.buildSuccessResult(null);
    }

    @PostMapping("delete")
    public Result delete(@RequestBody User user) {
        userService.delete(user);
        return ResultFactory.buildSuccessResult(null);
    }

    @GetMapping("findAll")
    public Result findAll() {
        List<User> userList = userService.findAll();
        return ResultFactory.buildSuccessResult(userList);
    }

    @GetMapping("getUser")
    public Result getUser(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return ResultFactory.buildSuccessResult(user);
    }
    @GetMapping("loggedout")
    public Result loggedout(HttpSession session){
        session.setAttribute("user",null);
        return ResultFactory.buildSuccessResult(null);
    }

    @PostMapping("reset")
    public Result resetPassword(@RequestBody User user){
        String trueCode;
        if (stringRedisService.hasKey(user.getPhone())) {
            trueCode = (String) stringRedisService.get(user.getPhone());
        } else {
            return ResultFactory.buildFailResult("验证码失效");
        }
        if (!trueCode.equals(user.getCode())) {
            return ResultFactory.buildFailResult("验证码错误");
        }else {
            userService.reset(user);
            return ResultFactory.buildSuccessResult(null);
        }
    }
    @PostMapping("avatar")
    public String coversUpload(MultipartFile file) throws Exception {
        String folder = "D:/workspace/img";
        File imageFolder = new File(folder);
        File f = new File(imageFolder, RandomUtil.randomBuilder(6) + file.getOriginalFilename()
                .substring(file.getOriginalFilename().length() - 4));
        String filename = file.getName();
        if (!f.getParentFile().exists())
            f.getParentFile().mkdirs();
        try {
            file.transferTo(f);
            String imgURL = "http://localhost:8085/api/file/" + f.getName();
            return imgURL;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }


}
